package cn.tansanqinger.dao.sql.group;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.IGroupSqlDao;
import cn.tansanqinger.dao.sql.IUserSqlDao;
import cn.tansanqinger.dao.sql.account.UserAccountSqlDaoImpl;
import cn.tansanqinger.pojo.Flick;
import cn.tansanqinger.pojo.User;

@Repository
public class GroupSqlDaoImpl {
	
	@Autowired
	private IGroupSqlDao dao;
	@Autowired
	private IUserSqlDao uDao;

	/**
	 *  查询数据，通过条件查询
	 * @param keywords
	 * @param isbanned
	 * @param starttime
	 * @param endtime
	 * @param page
	 * @param limit
	 * @return
	 */
	public Map<String, Object> selectMessage(String keywords, int isbanned, long starttime, long endtime, long reportStartTime, long reportEndTime, int page, int limit , int isreport){
		QueryWrapper<Flick> queryWrapper = new QueryWrapper<Flick>();
		if(!"".equals(keywords)) {
			queryWrapper.and(wrapper -> wrapper.like("gname", keywords).or().like("account", keywords).or().like("guse", keywords));
		}
		if(isbanned!=-1) {//是否被封禁
			queryWrapper.lambda().eq(Flick::getIsbanned, isbanned);
		}
		if(starttime!=-1&&endtime!=-1) {
			queryWrapper.lambda().ge(Flick::getGtime,starttime).le(Flick::getGtime,endtime);
		}
		if(reportStartTime!=-1&&reportEndTime!=-1) {
			queryWrapper.lambda().ge(Flick::getReporttime,reportStartTime).le(Flick::getReporttime,reportEndTime);
		}
		if(isreport==1) {
			queryWrapper.lambda().eq(Flick::getIsreport, isreport);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("count", dao.selectCount(queryWrapper));
		Page<Flick> pg = new Page<Flick>(page, limit);
		IPage<Flick> iPage = dao.selectPage(pg, queryWrapper);
		List<Flick> coursesList = iPage.getRecords();
		for (int i = 0; i < coursesList.size(); i++) {
			User user = uDao.selectById(coursesList.get(i).getGuid());
			if(user!=null) {
				coursesList.get(i).setUname(user.getName());
				coursesList.get(i).setUaccount(user.getAccount());
			} else {
				coursesList.remove(i);
			}
		}
		resultMap.put("data", coursesList);
		resultMap.put("code","0");
		resultMap.put("msg", "查询成功");
		return resultMap;
	}
	
	
	/**
	 * 查询信息
	 * @param cid
	 * @return
	 */
	public Flick selectOnt(int id) {
		return dao.selectById(id);
	}
	
	
	/**
	 * 删除信息
	 * @param cid
	 * @return
	 */
	public int deleteOne(int id) {
		return dao.deleteById(id);
	}
	
	/**
	 * 修改信息
	 * @param user
	 * @return
	 */
	public int modification(Flick object) {
		return dao.updateById(object);
	}
}
